package com.wswy.wyjk.ui.main.choose

import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import com.raft.framework.ui.adapter.SampleRecyclerViewAdapter
import com.timehop.stickyheadersrecyclerview.StickyRecyclerHeadersAdapter
import com.wswy.wyjk.R
import com.wswy.wyjk.model.CityModel
import com.wswy.wyjk.model.ICity
import com.wswy.wyjk.ui.widget.HotCityLayout
import kotlinx.android.synthetic.main.layout_list_item_city.view.*

class ChooseCityAdapter : SampleRecyclerViewAdapter<ICity, ChooseCityAdapter.ViewHolder>(), StickyRecyclerHeadersAdapter<ChooseCityAdapter.HeaderViewHolder> {

    companion object {

        private const val TYPE_HOT = 1
        private const val TYPE_CITY = 2
    }


    var topHotClickListener : ((ICity)->Unit)?=null

    override fun getHeaderId(position: Int): Long {
        if (position == 0) return -1
        return getItemData(position)?.getHeaderId()?:0
    }

    override fun onCreateHeaderViewHolder(parent: ViewGroup?): HeaderViewHolder {
        return HeaderViewHolder(LayoutInflater.from(parent?.context).inflate(R.layout.layout_item_car_header,parent,false))
    }

    override fun onBindHeaderViewHolder(holder: HeaderViewHolder?, position: Int) {
        holder?.tvTitle?.text = getItemData(position)?.nameLatter
    }

    override fun itemClickable(position: Int): Boolean {
        return position != 0;
    }

    override fun getItemViewType(position: Int): Int {
        return if (position == 0) TYPE_HOT else TYPE_CITY
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val inflater = LayoutInflater.from(parent.context)

         return when(viewType){
            TYPE_HOT -> HotViewHolder(inflater.inflate(R.layout.item_layout_hot_city,parent,false),datas,topHotClickListener)
            TYPE_CITY -> ViewHolder(inflater.inflate(R.layout.layout_list_item_city,parent,false))
             else -> ViewHolder(inflater.inflate(R.layout.layout_list_item_city,parent,false))
        }
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int, data: ICity?) {
        holder.itemView.tv_name?.text = data?.cityName
    }


    open class ViewHolder(itemView:View) : RecyclerView.ViewHolder (itemView){

    }


    class HotViewHolder(itemView: View, val dataList:List<ICity>, topHotClickListener:((ICity)->Unit)?):ViewHolder(itemView){


        init {

            val cities = arrayListOf<ICity>(CityModel("北京","beijing"),
                CityModel("上海","shanghai"),
                CityModel("广州","guangzhou"),
                CityModel("深圳","shenzhen"),
                CityModel("南京","nanjing"),
                CityModel("苏州","suzhou"))

            (itemView as? HotCityLayout)?.let {

                it.topHotClickListener = topHotClickListener

                it.setHotCity(cities,dataList)
            }

        }

    }



    class HeaderViewHolder (itemView:View): RecyclerView.ViewHolder(itemView){
        var tvTitle: TextView = itemView as TextView

    }
}